import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
/**
 * 已修复漏洞
 */

@Component({
    templateUrl: './security.html',
    styleUrls:['security.scss'],
})
export class SecurityComponent implements OnInit {
    dataList = [];
    count= {};
    routerData;
    childComponent;
    ngOnInit() {
        this.apiService.post('数据权限设置统计数',).then((res:any)=>{
            this.count = res.data;
        })
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService,
        private activatedRoute:ActivatedRoute
    ) { 
        let temp = this.activatedRoute;
        while (temp.firstChild) {
            temp = temp.firstChild;
        }
        this.routerData = temp.url['value'][0];
    }

    goto(text){
        if(this.routerData.path !== text){
            this.router.navigateByUrl('data/security/'+text);
            this.routerData = {path:text};
        }
    }

    updateSecurity(){
        const dataList = this.childComponent.dataList;
        const promiseList = [];
        dataList.forEach(element => {
            if(element.cz){
                const ids = element.list.filter((e:any)=>e.checked).map((e:any)=>e.id);
                promiseList.push(this.apiService.post('批量设置数据权限',{ids,status:element.cz}));
            }
        });
        Promise.all(promiseList).then((res:any)=>{
            this.nzMessageService.success('操作成功！');
            this.childComponent.loadList();
        })
    }
}
